package org.example.myleet.p606;

import org.example.myleet.Utils.TreeNode;

public class Solution {

    public String tree2str(TreeNode root) {
        if (null == root) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(root.val);
        if (null == root.left && null == root.right) {
            return sb.toString();
        } else {
            if (null != root.left) {
                sb.append('(').append(tree2str(root.left)).append(')');
            } else {
                sb.append('(').append(')');
            }
            if (null != root.right) {
                sb.append('(').append(tree2str(root.right)).append(')');
            }
        }
        return sb.toString();
    }
}
